package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import model.QandA;
import model.User;
import org.springframework.stereotype.Component;
import service.AskDoctorService;
import service.UserService;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 10/1/12
 * Time: 7:19 PM
 * To change this template use FileModel | Settings | FileModel Templates.
 */

public class  LoginAction extends ActionSupport implements ModelDriven{

    //service class
    UserService userService;
    AskDoctorService askDoctorService;
    int answeredQuestion;
    User user = new User();


    public Object getModel() {
        return getUser();  //To change body of implemented methods use FileModel | Settings | FileModel Templates.
    }

    private String name;
    private String password;
//private IUserService userService;

    public String loginUser() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();

//create a new session when a user login
       /* session.put("USER_ID", getName());
//Spring calls setUserService() method to create a UserService instance, see the following three lines in the Spring configuration file applicationContext.xml
//<bean id=”loginActionClass” class=”com.wordpress.bioincloud.LoginAction” scope=”prototype”>
//<property name=”userService” ref=”userService” />
//</bean> */

        System.out.println("user Name " +user.getName() + user.getPassword());

        if (userService.loginUser(user).size() > 0) {
            session.put("USER_ID", getName());
            session.put("USER", user);
            answeredQuestion = askDoctorService.getAnsweredQues(user).size();
            return "success";
        } else {
            this.addActionError("Wrong user name or password, try it again.");
            return "error";
        }

    }


    public String loginScreen(){
        return "login";
    }

    //validate the user input
/*   public void validate() {
        if (getName().length() == 0) {
            this.addFieldError("name", "User name is is required"+getPassword());
//addActionError(“User name is required”);
        }
        if (getPassword().length() == 0) {
            this.addFieldError("password", "Password is is required"+getName());
        } *//*else if (userService.loginUser(name, password).size() == 0) {
//System.out.println(“************************”);
            this.addActionError("Wrong user name or password, try it again.");

//System.out.println(“************************”);
        }*//*
    }*/


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public AskDoctorService getAskDoctorService() {
        return askDoctorService;
    }

    public void setAskDoctorService(AskDoctorService askDoctorService) {
        this.askDoctorService = askDoctorService;
    }

    public int getAnsweredQuestion() {
        return answeredQuestion;
    }

    public void setAnsweredQuestion(int answeredQuestion) {
        this.answeredQuestion = answeredQuestion;
    }
}
